package com.bank.conrtoller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bank.excption.CommonException;


public class BaseController  {
	 Logger logger = Logger.getLogger(BaseController.class);
	 HttpServletRequest baseRequst;
		
	private   Map<String,Object>  returnMap = new HashMap<String, Object>();
	
	public  BaseController(){
		
		
	}
	
	public void  setSuccess(){
		returnMap.put("returncode", "success");
	}
	public void  setFailed(){
		returnMap.put("returncode","failed");
	}
	
	public Map<String,Object> setAndGetSuccess(){
		returnMap.put("returncode", "success");
		return returnMap;
	}  
	
	public Map<String, Object> setAndGetFailed(){
		returnMap.put("returncode", "failed");
		return returnMap;
	}
	
	public void setMapValue(String key, Object value){
		returnMap.put(key,value);
	}
	
	public void setMapListValue(String key, List<Object> value)
	{
		returnMap.put(key, value);
	}
	
	public Map<String,Object> returnMap(){
		return  returnMap;
	}
	
	 /** 基于@ExceptionHandler异常处理 */
    @ExceptionHandler({CommonException.class}) 
    @ResponseBody
    public Map<String, Object>  handleAndReturnData(HttpServletRequest request, HttpServletResponse response, Exception ex) {

        Map<String, Object> data = new HashMap<String, Object>();
        if(ex instanceof CommonException) {
        	CommonException e = (CommonException)ex;
        	logger.debug("code"+e.getRespCode());
        	logger.debug("msg"+e.getMessage());
            data.put("code", e.getRespCode());
            data.put("msg", e.getMessage());
        }
        data.put("returncode", "failed");
       
        return data;
    }
	
}
